package com.snapp.ejb;

import java.util.HashMap;
import java.util.Vector;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import com.snapp.ejb.interfaces.local.SNAppManagerLocal;
import com.snapp.ejb.interfaces.remote.SNAppManagerRemote;
import com.snapp.util.factory.Factory;
import com.snapp.util.object.model.ObjectModel;

@Stateless(name="SNAppManagerBean")
@Remote(SNAppManagerRemote.class)
@Local(SNAppManagerLocal.class)
public class SNAppManagerBean implements SNAppManagerLocal, SNAppManagerRemote {

	/*
	 * Load a ObjectModel based on the object identifier, and the id given.
	 */
	public ObjectModel load(String snappGlobalId, HashMap<String,String> params) {
		ObjectModel model = getObjectModelInfo(snappGlobalId);
		// load using default identifier
		model.load(params);
		return model;
	}

	/*
	 * Save a ObjectModel based on the object identifier, using the given parameters.
	 */
	public ObjectModel save(String snappGlobalId, HashMap<String,Object> params) {
		ObjectModel model = getObjectModelInfo(snappGlobalId);
		// save..
		model.save(params);
		return model;
	}

	/*
	 * Save a ObjectModel list based on the object identifier, using the given parameters.
	 */
	public ObjectModel saveList(String snappGlobalId, HashMap<String,Object> params, String delimiter) {
		ObjectModel model = getObjectModelInfo(snappGlobalId);
		// save..
		model.saveList(params,delimiter);
		return model;
	}

	/*
	 * Retrieve a list of ObjectModels based on the given filter.
	 */
	public Vector<ObjectModel> list(String snappGlobalId, HashMap<String,String> params) {
		ObjectModel model = getObjectModelInfo(snappGlobalId);
		// fetch list..
		Vector<ObjectModel> list = model.list(params);
		return list;
	}
	
	/*
	 * Retrieve the count based on the given filter.
	 */
	public int count(String snappGlobalId, HashMap<String,String> params) {
		ObjectModel model = getObjectModelInfo(snappGlobalId);
		// fetch list..
		int count = model.count(params);
		return count;
	}

	/*
	 * Delete an object/list based on given filter
	 * @see com.snapp.ejb.interfaces.snappManager#delete(java.lang.String, java.util.HashMap)
	 */
	public void delete(String snappGlobalId, HashMap<String, String> params) {
		ObjectModel model = getObjectModelInfo(snappGlobalId);
		model.delete(params);
	}

	/*
	 * Load a ObjectModel based on the object identifier, and the id given.
	 */
	public ObjectModel load(String snappGlobalId, String params) {
		ObjectModel model = getObjectModelInfo(snappGlobalId);
		// load using default identifier
		model.load(params);
		return model;
	}

	/*
	 * Retrieve a list of ObjectModels based on the given filter.
	 */
	public Vector<ObjectModel> list(String snappGlobalId, String params) {
		ObjectModel model = getObjectModelInfo(snappGlobalId);
		// fetch list..
		Vector<ObjectModel> list = model.list(params);
		return list;
	}
	
	/*
	 * Retrieve the count based on the given filter.
	 */
	public int count(String snappGlobalId, String params) {
		ObjectModel model = getObjectModelInfo(snappGlobalId);
		// fetch list..
		int count = model.count(params);
		return count;
	}

	/*
	 * Delete an object/list based on given filter
	 * @see com.snapp.ejb.interfaces.SNAppManager#delete(java.lang.String, java.util.HashMap)
	 */
	public void delete(String snappGlobalId, String params) {
		ObjectModel model = getObjectModelInfo(snappGlobalId);
		model.delete(params);
	}

	/**
	 * Utility to get the bean representing the ObjectModel record for the give global id
	 */
	public ObjectModel getObjectModelInfo(String snappGlobalId) {
		
		ObjectModel tmp = Factory.getObject(snappGlobalId);
		// get object info..
		ObjectModel obj = tmp.copy();
		
		return obj;
	}

}
